package cn.chrisn.componenthub.Controller;

import cn.chrisn.componenthub.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Description
 * @Author Chrisn
 * @Date 2025/5/3
 */
@Slf4j
@RestController
public class SessionController {
    /**
     * 设置cookie
     * @param response HttpservletResponse对象
     */
    @GetMapping("/setCookie")
    public void setCookie(HttpServletResponse response){
        response.addCookie(new Cookie("useName","chrisn"));
    }

    /**
     * 获取cookie
     * @param request
     */
    @GetMapping("/getCookie")
    public void getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("userName")){
                log.info("userName:{}",cookie.getValue());
            }
        }
    }

        @GetMapping("/s1")
        public void session1(HttpSession session) throws Exception {
            log.info("HttpSession-s1: {}", session.hashCode());
            session.setAttribute("loginUser", "tom"); //往session中存储数据
            throw new Exception("报错了");
        }

        @GetMapping("/s2")
        public void session2(HttpServletRequest request){
            HttpSession session = request.getSession();
            log.info("HttpSession-s2: {}", session.hashCode());
            Object loginUser = session.getAttribute("loginUser"); //从session中获取数据
            log.info("loginUser: {}", loginUser);
        }
    }

